package com.aw.baselib.base;

import com.aw.baselib.net.callback.ApiBaseCallback;
import com.aw.baselib.net.callback.ApiCustomBaseCallback;
import com.aw.baselib.net.callback.AwApiCallback;
import com.aw.baselib.net.callback.AwCustomApiCallback;
import com.aw.baselib.net.callback.AwCustomRspCallback;
import com.aw.baselib.net.callback.AwCustomRspSubscriber;
import com.aw.baselib.net.callback.AwErrorCallback;
import com.aw.baselib.net.callback.AwErrorSubscriber;
import com.aw.baselib.net.callback.AwRspCallback;
import com.aw.baselib.net.callback.AwRspSubscriber;
import com.aw.baselib.util.AwLog;
import com.google.gson.Gson;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author : hzw
 */

public class AwCommonPresenter implements AwBasePresenter {

    public Gson mGson = new Gson();
    protected CompositeDisposable compositeDisposable;

    public void attachView() {
        compositeDisposable = new CompositeDisposable();
    }

    public void detachView() {
        if (compositeDisposable != null) {
            compositeDisposable.dispose();
            compositeDisposable = null;
        }
    }

    /**
     * 添加 rxJava 发出的请求
     */
    private void addDisposable(Disposable disposable) {
        if (disposable == null || disposable.isDisposed()) {
            disposable = new CompositeDisposable();
        }
        if(compositeDisposable != null) {
            compositeDisposable.add(disposable);
        }
    }

    protected void addIOSubscription(Observable observable, AwRspSubscriber response, AwErrorSubscriber error) {
        addDisposable(observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response, error));
    }

    protected <T> void addIOSubscription(Observable observable, ApiBaseCallback<T> apiCallback, int... count) {
        apiCallback.onStart();
        addDisposable(observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new AwRspSubscriber(new AwRspCallback<T>() {

                    @Override
                    public void onSuccess(T data) {
                        apiCallback.onSuccess(data);
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        AwLog.d("addIOSubscription onFailure code: " + code + " ,msg: " + msg);
                        apiCallback.onFail(code, msg);
                    }

                    @Override
                    public void onCompleted() {
                        apiCallback.onCompleted();
                    }
                }), new AwErrorSubscriber(new AwErrorCallback() {
                    @Override
                    public void onFailure(int code, String msg) {
                        AwLog.d("addIOSubscription AwErrorSubscriber code: " + code + " ,msg: " + msg);
                        apiCallback.onFail(code, msg);
                    }
                })));
    }

    protected <T> void addIOSubscription(Observable observable, AwApiCallback<T> apiCallback, int... count) {
        apiCallback.onStart();
        addDisposable(observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new AwRspSubscriber(new AwRspCallback<T>() {

                    @Override
                    public void onSuccess(T data) {
                        apiCallback.onSuccess(data);
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        AwLog.d("addIOSubscription onFailure code: " + code + " ,msg: " + msg);
                        apiCallback.onFail(code, msg);
                    }

                    @Override
                    public void onCompleted() {
                        apiCallback.onCompleted();
                    }
                }), new AwErrorSubscriber(new AwErrorCallback() {
                    @Override
                    public void onFailure(int code, String msg) {
                        AwLog.d("addIOSubscription AwErrorSubscriber code: " + code + " ,msg: " + msg);
                        apiCallback.onFail(code, msg);
                    }
                })));
    }

    protected <T> void addIOCustomSubscription(Observable observable, ApiCustomBaseCallback<T> apiCallback, int... count) {
        apiCallback.onStart();
        addDisposable(observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new AwCustomRspSubscriber(new AwCustomRspCallback<T>() {

                    @Override
                    public void onSuccess(T data) {
                        apiCallback.onSuccess(data);
                    }

                    @Override
                    public void onFailure(Object code, String msg) {
                        AwLog.d("addIOSubscription onFailure code: " + code + " ,msg: " + msg);
                        apiCallback.onFail(code, msg);
                    }

                    @Override
                    public void onCompleted() {
                        apiCallback.onCompleted();
                    }
                }), new AwErrorSubscriber(new AwErrorCallback() {
                    @Override
                    public void onFailure(int code, String msg) {
                        AwLog.d("addIOSubscription AwErrorSubscriber code: " + code + " ,msg: " + msg);
                        apiCallback.onFail(code, msg);
                    }
                })));
    }

    protected <T> void addIOCustomSubscription(Observable observable, AwCustomApiCallback<T> apiCallback, int... count) {
        apiCallback.onStart();
        addDisposable(observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new AwCustomRspSubscriber(new AwCustomRspCallback<T>() {

                    @Override
                    public void onSuccess(T data) {
                        apiCallback.onSuccess(data);
                    }

                    @Override
                    public void onFailure(Object code, String msg) {
                        AwLog.d("addIOSubscription onFailure code: " + code + " ,msg: " + msg);
                        apiCallback.onFail(code, msg);
                    }

                    @Override
                    public void onCompleted() {
                        apiCallback.onCompleted();
                    }
                }), new AwErrorSubscriber(new AwErrorCallback() {
                    @Override
                    public void onFailure(int code, String msg) {
                        AwLog.d("addIOSubscription AwErrorSubscriber code: " + code + " ,msg: " + msg);
                        apiCallback.onFail(code, msg);
                    }
                })));
    }


    public RequestBody getBody(Object object) {
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), mGson.toJson(object));
    }
}
